package com.azusa.xbiao.service.impl;

import com.azusa.xbiao.dao.AddressDao;
import com.azusa.xbiao.dao.UserDao;
import com.azusa.xbiao.entity.Address;
import com.azusa.xbiao.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressDao addressDao;

    @Autowired
    private UserDao userDao;


    @Override
    public List<Address> findUserAll(Integer id) {
        return addressDao.findByUserId(id);
    }

    @Override
    public void deleteAddressById(int id) {
        addressDao.deleteById(id);
    }

    @Override
    public void updateDefaults(int aid, int uid) {
        List<Address> list = addressDao.findByUserId(uid);
        for (Address address : list) {
            if (address.getDefault() == 1){
                addressDao.updateByDefault(address.getId(),0);
            }
            if (address.getId() == aid){
                addressDao.updateByDefault(address.getId(),1);
                userDao.updateUserAddress(uid,aid,address.getRegion()+address.getAddress());
            }
        }
    }

    @Override
    public void insertAddress(int uid, Map<String, String> params) {
        Address address = new Address();
        address.setName(params.get("name"));
        address.setPhone(params.get("phone"));
        address.setAddress(params.get("address"));
        address.setRegion(params.get("region"));
        address.setPostcode("555555");
        address.setDisplay(0);
        address.setuId(uid);
        //新添加的是默认地址
        if (params.containsKey("isDefault") && params.get("isDefault").equals("1")){
            address.setDefault(1);
            //查询是否已经有默认地址
            List<Address> list = addressDao.findByUserId(uid);
            //如果有则修改为非默认地址
            if (list.size() > 0 && list.get(0).getDefault()==1) {
                addressDao.updateByDefault(list.get(0).getId(),0);
            }
        }else {
            address.setDefault(0);
        }
        Address saveAddress = addressDao.save(address);
        //修改用户的信息页的默认地址
        if (saveAddress.getDefault() == 1){
            userDao.updateUserAddress(uid,saveAddress.getId(),saveAddress.getRegion()+saveAddress.getAddress());
        }
    }

    @Override
    public Address findAddressById(int aid, int uid) {
        return addressDao.findByUIdAndId(aid,uid);
    }

    @Override
    public int updateAddress(Address address) {
        //修改的地址是默认地址
        if (address.getDefault() == 1){
            //查询是否已经有默认地址
            List<Address> list = addressDao.findByUserId(address.getuId());
            //如果有且不是当前的地址则将默认地址修改为非默认地址
            if (list.size() > 0 && list.get(0).getDefault()==1 && list.get(0).getId() != address.getId()) {
                addressDao.updateByDefault(list.get(0).getId(),0);
                //修改其用户信息的默认地址
                userDao.updateUserAddress(address.getuId(),address.getId(),address.getRegion() + address.getAddress());
            }
        }
        addressDao.save(address);
        return 1;
    }

}
